package it.traveldream.connection.beans;

import it.traveldream.exception.UtenteNonTrovatoException;
import it.traveldreamClient.beans.UserMgr;
import it.traveldreamClient.beans.dto.ComponenteDTO;
import it.traveldreamClient.beans.dto.UserDTO;

import java.util.LinkedList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;


@ManagedBean(name="UserBean")
@SessionScoped
public class UserBean {
	
	@EJB
	private UserMgr userMgr;
	
	private UserDTO selectedUser;
	private List<String> selectedGifts;
	List<String> giftList;
	private String input;
	
	public String findUtente() {
		try {
			selectedUser= userMgr.findUtente(input);
			for(int i=0; i<selectedUser.getGiftList().size();i++){
				ComponenteDTO c= selectedUser.getGiftList().get(i);
				if(!(c.isRegalo()&&c.isDisponibilita())){
					giftList.add(c.toString());
				}
				
			}
			return "/user/otherUserGiftList?faces-redirect=true";
		} catch (UtenteNonTrovatoException e) {
			return "/user/errore?faces-redirect=true";
		}
		
		
	}
	
	public UserBean (){
		selectedUser= new UserDTO();
		selectedGifts=new LinkedList<String>();
		giftList=new LinkedList<String>();
	}
	
	public void regala(ActionEvent actionEvent){
		List <ComponenteDTO> regali= new LinkedList<ComponenteDTO>();
		for(ComponenteDTO cmp : selectedUser.getGiftList()){
			if(selectedGifts.contains(cmp.toString())){
				regali.add(cmp);
				giftList.remove(cmp.toString());
				selectedUser.getGiftList().remove(cmp);
			}
		}
		System.out.println(selectedUser.getGiftList());
		userMgr.regala(selectedUser,regali);
	}
	
	public String getName() {
		return userMgr.getUserDTO().getCognome();
	}

	

	public UserDTO getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(UserDTO selectedUser) {
		this.selectedUser = selectedUser;
	}

	public List<String> getSelectedGifts() {
		return selectedGifts;
	}

	public void setSelectedGifts(List<String> selectedGifts) {
		this.selectedGifts = selectedGifts;
	}

	public String getInput() {
		return input;
	}

	public void setInput(String input) {
		this.input = input;
	}

	public List<String> getGiftList() {
		return giftList;
	}

	public void setGiftList(List<String> giftList) {
		this.giftList = giftList;
	}

	
	
}
